import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  HostBinding,
  OnInit,
} from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { slideToRight } from 'src/app/anims';
import { ConfirmDialogComponent } from 'src/app/shared';
import { CopyTaskComponent } from '../copy-task';
import { NewTaskComponent } from '../new-task';
import { NewTaskListComponent } from '../new-task-list';

@Component({
  selector: 'app-task-home',
  templateUrl: './task-home.component.html',
  styleUrls: ['./task-home.component.scss'],
  animations: [slideToRight],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TaskHomeComponent implements OnInit {
  @HostBinding('@routeAnim') state;

  lists = [
    {
      id: 1,
      name: '代办',
      order: 1,
      tasks: [
        {
          id: 1,
          desc: '任务一：去星巴克买杯咖啡',
          completed: false,
          owner: {
            id: 1,
            name: '张三',
            avatar: 'avatars:svg-11',
          },
          dueDate: new Date(),
          reminder: true,
          priority: 3,
        },
        {
          id: 2,
          desc: '任务二：完成老师布置的 ppt 作业',
          completed: true,
          owner: {
            id: 1,
            name: '李四',
            avatar: 'avatars:svg-12',
          },
          dueDate: new Date(),
          priority: 2,
        },
      ],
    },
    {
      id: 2,
      name: '进行中',
      order: 2,
      tasks: [
        {
          id: 1,
          desc: '任务三：项目代码评审',
          completed: false,
          // owner: {
          //   id: 1,
          //   name: '王五',
          //   avatar: 'avatars:svg-13',
          // },
          priority: 1,
          dueDate: new Date(),
        },
        {
          id: 2,
          desc: '任务四：制定项目计划',
          completed: false,
          owner: {
            id: 1,
            name: '李四',
            avatar: 'avatars:svg-12',
          },
          priority: 2,
          dueDate: new Date(),
        },
      ],
    },
  ];

  constructor(private dialog: MatDialog, private cd: ChangeDetectorRef) {}

  ngOnInit(): void {}

  launchNewTaskDialog(): void {
    this.dialog.open(NewTaskComponent, {
      data: {
        title: '新建任务',
      },
    });
  }

  launchCopyTaskDialog(): void {
    this.dialog.open(CopyTaskComponent, {
      data: {
        lists: this.lists,
      },
    });
  }

  launchDataTaskDialog(task): void {
    this.dialog.open(NewTaskComponent, {
      data: {
        title: '修改任务',
        task,
      },
    });
  }

  launchConfirmDialog(): void {
    const dialogRef = this.dialog.open(ConfirmDialogComponent, {
      data: {
        title: '删除项目：',
        content: '确认要删除该任务列表？',
      },
    });

    dialogRef.afterClosed().subscribe((result) => console.log(result));
  }

  launchEditListDialog(): void {
    const dialogRef = this.dialog.open(NewTaskListComponent, {
      data: {
        title: '更改列表名称：',
      },
    });

    dialogRef.afterClosed().subscribe((result) => console.log(result));
  }

  launchNewListDialog(): void {
    const dialogRef = this.dialog.open(NewTaskListComponent, {
      data: {
        title: '新建列表名称：',
      },
    });

    dialogRef.afterClosed().subscribe((result) => console.log(result));
  }

  handleMove(srcData, list): void {
    switch (srcData.tag) {
      case 'task-item':
        console.log('handle item');
        break;
      case 'task-list':
        console.log('handle list');
        [list.order, srcData.data.order] = [srcData.data.order, list.order];
        break;
      default:
        break;
    }
  }

  handleQuickTask(desc): void {
    console.log(desc);
  }
}
